package com.visikard.asynctask;

import com.visikard.application.Constant;
import com.visikard.application.MyApplication;
import com.visikard.application.UIMessage;
import com.visikard.business.AccountBUS;
import com.visikard.item.VKregisterSession;
import com.visikard.response.BaseResponse;
import com.visikard.response.GetProfileResponse;

import android.os.Handler;
import android.util.Log;

public class GetUserProfileTask extends BaseTask {

	public GetUserProfileTask(Handler handler) {
		super(handler);
	}
	
	public GetUserProfileTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	@Override
	protected Object doInBackground(Object... params) {
		AccountBUS busObj = new AccountBUS();
		//parse user default				
		return busObj.parseProfileDefault();
	}

	@Override
	protected boolean handleResult(Object result) {
		GetProfileResponse getProfileResponse = (GetProfileResponse)result;
		if(getProfileResponse != null){
			if(getProfileResponse.getErrorCode() == BaseResponse.SUCCESS){
				Constant.defaultProfile = getProfileResponse.getUserProfile();
				Constant.idKard = Constant.defaultProfile.getIdKards();
				Constant.userVisible = getProfileResponse.getUserVisible();
				
				VKregisterSession vksession = new VKregisterSession();
				vksession.setUsername(Constant.username);
				vksession.setPassword(Constant.password);
				vksession.setUserId(String.valueOf(Constant.uid));
				vksession.setLoginSession(Constant.loginSession);
				vksession.setKardId(String.valueOf(Constant.idKard));
				MyApplication.instance.saveSession(vksession);
				mUIHandler.sendEmptyMessage(UIMessage.MSG_GET_USER_PROFILE_COMPLETE);
			}else{
				Log.e(Constant.LOG_TAG, "LoginTask: parseProfileDefault " + getProfileResponse.getErrorMsg());
				mUIHandler.sendEmptyMessage(UIMessage.MSG_GET_USER_PROFILE_FAILED);
			}
			return true;
		}
		return false;
	}
	
	

}
